package com.nenglai.admin.modular.report.controller;

import com.alibaba.fastjson.JSONObject;
import com.nenglai.admin.modular.report.form.OutputForm;
import com.nenglai.admin.modular.report.service.ReportService;
import com.nenglai.common.jwt.VerifyToken;
import com.nenglai.common.response.ResultGenerator;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author kongdehui
 * @date 2021/06/18 14:45
 **/
@RestController
@RequestMapping("backend/report")
public class ReportController {

    @Resource
    ReportService reportService;

    @VerifyToken
    @PostMapping("output")
    public String output(@RequestBody OutputForm form) throws Exception {
        Object output = reportService.output(form.getId(), form.getParams());
        return JSONObject.toJSONString(ResultGenerator.success(output));
    }

    @GetMapping("download")
    public String download(@RequestParam String reportId, HttpServletResponse response)
            throws IOException, ClassNotFoundException {
        reportService.download(reportId, response);
        return "success";
    }

}
